package com.kingtroldata.gyjk_demo;

import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

/**
 * Created by user on 2017/2/15.
 */

public class ScreenKit {
    private static ScreenKit kit;
    private View bg;//底图
    private float bg_width;//底图显示的宽
    private float bg_height;//底图显示的高
    private float design_width;//底图
    private float design_height;//底图
    private float scale_w;//宽的压缩比
    private float scale_h;//高的压缩比
    private float skew_x;//x轴偏移量
    private float skew_y;//y轴偏移量

    public static ScreenKit init(View bg, float design_width, float design_height, float skew_x, float skew_y) {
        if (kit == null) {
            kit = new ScreenKit(bg, design_width, design_height, skew_x, skew_y);
        }
        return kit;
    }

    private ScreenKit(View bg, float design_width, float design_height, float skew_x, float skew_y) {
        this.bg = bg;
        this.design_width = design_width;
        this.design_height = design_height;
        this.skew_x = skew_x;
        this.skew_y = skew_y;
        initSize();
    }

    //初始化底图的尺寸
    private void initSize() {
        ViewTreeObserver vto = bg.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                bg_width = bg.getWidth();
                bg_height = bg.getHeight();
                LoggerUtils.i("======================================");
                LoggerUtils.i("底图的宽高为:  bg_height:" + bg_height + ";bg_width:" + bg_width);
                if (bg_height != 0 && bg_width != 0) {
                    bg.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    getScale();
                    if (successListener != null) {
                        successListener.onMeasureSuccess();
                    }
                }
            }
        });
    }

    /**
     * 获取x轴,y轴的缩放比例
     */
    private void getScale() {
        scale_w = design_width / bg_width;
        scale_h = design_height / bg_height;
        LoggerUtils.i("======================================");
        LoggerUtils.i("x轴缩放比:" + scale_w + ";y轴缩放比:" + scale_h);
    }

    /**
     * 转换view的宽高和坐标
     *
     * @param w 宽
     * @param h 高
     * @param x x坐标
     * @param y y坐标
     */
    public View transformView(View v, float w, float h, float x, float y) {

        int w_t = (int) (w / scale_w);
        int h_t = (int) (h / scale_h);
        float x_t = x / scale_w;
        float y_t = y / scale_h;
        LoggerUtils.i("======================================");
        LoggerUtils.i("变换后图片宽:" + w_t + "\r\n" + "变换后图片高:" + h_t + "\r\n" + "变换后X轴坐标:" + x_t + "\r\n" + "变换后Y轴坐标:" + y_t);

        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(w_t, h_t);
        v.setLayoutParams(params);
        v.setX(x_t);
        v.setY(y_t);
        return v;
    }

    /**
     * 添加测量成功的回调监听
     *
     * @param successListener
     */
    public ScreenKit setOnMessureSuccessListener(MeasureSuccessCallBack successListener) {
        this.successListener = successListener;
        return kit;
    }

    MeasureSuccessCallBack successListener;

    interface MeasureSuccessCallBack {
        void onMeasureSuccess();
    }


}
